package service

import (
	"awesomeTask/application/variable"
	"awesomeTask/config"
	"awesomeTask/system/dds"
	"awesomeTask/system/event"
	"awesomeTask/system/helper"
	"awesomeTask/system/kernel"
)

type DdsService struct {
}

func (ioc DdsService) Register() {
	instance := kernel.GetIocInstance()

	instance.BindCreateObject("dds", func() interface{} {
		dds := dds.DdsManagerObj{
			MaxBatchSize:        3000,
			CopyNum:             2,
			WaitPersistenceChan: make(chan interface{}),
			DdsPath:             config.GetConfigByKey("ddsPath").(string),
		}
		go dds.Persistence()
		return &dds
	}, true)
	dds.GetDdsObj().RecoverDataByLocal()
	event.Register(variable.ON_DDS_SAVE, func(args ...interface{}) {
		data := args[0].(map[string]interface{})
		dds.GetDdsObj().SaveDDL(helper.JsonEncode(data["data"]), data["key"].(string), data["ddsMap"].(map[string]interface{}))
	})
	event.Register(variable.ON_DDS_ASK_DATA, func(args ...interface{}) interface{} {
		data := args[0].(map[string]interface{})
		key := data["key"].(string)
		return dds.GetDdsObj().GetRecordByKey(key)
	})
	//timer.SetTimeInterval(func() {
	//	if variable.IsMaster {
	//		data := make([]interface{}, 0)
	//		for i := 0; i < 100000; i++ {
	//			data = append(data, map[string]interface{}{
	//				"index": i,
	//			})
	//		}
	//
	//		dds.GetDdsObj().Push(data, "testkey")
	//	}
	//}, 10)
}
func (ioc DdsService) Boot() {

}
